package com.yf.controller.api.course;

import com.yf.annotation.LoginUser;
import com.yf.entity.UserEntity;
import com.yf.entity.course.CourseEntity;
import com.yf.entity.course.CourseTypeEntity;
import com.yf.entity.course.LessonEntity;
import com.yf.entity.course.TopCourseEntity;
import com.yf.response.R;
import com.yf.service.course.*;
import com.yf.vo.LessonRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/course")
public class ApiCourseHomeController {

    @Autowired
    private TopCourseService topCourseService;

    @Autowired
    private CourseTypeService courseTypeService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private LessonService lessonService;

    @Autowired
    private LessonRecordService lessonRecordService;


    @GetMapping("/top")
    public R<List> topCourse() {
        List<TopCourseEntity> list = topCourseService.getAll();
        return R.ok(list);
    }


    @GetMapping("/type")
    public R<List> courseType() {
        List<CourseTypeEntity> list = courseTypeService.getAll();
        return R.ok(list);
    }


    @GetMapping("/list")
    public R<List> course() {
        List<CourseEntity> list = courseService.getAll();
        return R.ok(list);
    }

    @PostMapping("/type/list")
    public R<List> getCourseByType(Long typeId) {
        List<CourseEntity> list = courseService.getCourseByType(typeId);
        return R.ok(list);
    }


    @GetMapping("/info")
    public R courseInfo(Long courseId) {
        CourseEntity courseEntity = courseService.get(courseId);
        return R.ok(courseEntity);
    }

    //章节
    @GetMapping("/lesson")
    public R lesson(@LoginUser UserEntity userEntity,Long courseId){
        List<LessonEntity> list = lessonService.getList(courseId,userEntity.getId());
        //课程的总课时
        Integer count = lessonService.getCount(courseId);
        return R.ok(list).putExtra("count",count);
    }


    /**
     * 课程记录
     * @return
     */
    @GetMapping("/lesson/record")
    public R lessonRecord(@LoginUser UserEntity userEntity){
        List<LessonRecordVO> list = lessonRecordService.getLessonRecord(userEntity.getId());
        return R.ok(list);
    }


    /**
     * 课程是否购买
     */
    @GetMapping("/paid")
    public R coursePaid(@LoginUser UserEntity userEntity, Long courseId){
        return R.ok(courseService.coursePaid(userEntity.getId(),courseId));
    }

    /**
     * 若课程已经全部购买，查询所有章节的视频地址
     */
    @GetMapping("/paid/lessonUrlList")
    public R lessonUrlList(@LoginUser UserEntity userEntity, Long courseId){
        return R.ok(lessonService.getLessonUrlByCourseId(courseId));
    }


    /**
     * 章节是否购买
     */
    @GetMapping("/lesson/paid")
    public R lessonPaid(@LoginUser UserEntity userEntity, Long lessonId){
        return R.ok(lessonService.lessonVideo(userEntity.getId(),lessonId));
    }

    /**
     * 章节详细信息
    */
    @GetMapping("/lesson/info")
    public R lessonInfo(@LoginUser UserEntity userEntity, Long lessonId){
        return R.ok(lessonService.getLesson(lessonId,userEntity.getId()));
    }

    /**
     * 展示共同学习的5个人
     */
    @GetMapping("/paid/users")
    public R paidUsers(@LoginUser UserEntity userEntity, Long lessonId){
        return R.ok(courseService.getPaidUsers(lessonId));
    }

    /**
     * 用户已经购买的章节
     */
    @GetMapping("/paid/lessonList")
    public R getPaidLessonList(@LoginUser UserEntity userEntity){
        return R.ok(lessonService.getPaidLessonList(userEntity.getId()));
    }

}
